<?php
namespace cubox\provider\template;

class Tag
{
    public static function cms(string $html,string $name):array
    {
        return self::parse($html,'cms:'.$name);
    }

    public static function parse($html,$name):array
    {
        $data  = array();
        
        //闭合的标签
        $re = '/<'.$name.'([^<]*)>([\s\S]*?)<\/'.$name.'>/i';
        preg_match_all ($re, $html, $match);
        foreach($match[0] as $k=>$v){
            $data [] = array(
                'param'=>self::param($match[1][$k]),
                'input'=>$match[0][$k],
                'content'=>$match[2][$k]
            );
        }
        //非非闭合的标签
        $re = '/<'.$name.'([^<]*)\/>/i';
        preg_match_all ($re, $html, $match);

        foreach($match[0] as $k=>$v){
            $data [] = array(
                'param'=>self::param($match[1][$k]),
                'input'=>$match[0][$k],
                'content'=>''
            );
        }
        
        return $data;
    }


    public static function param(string $str):array{
        $re = '/\S+\s*=\s*\"[^\"]*\"/i';

        preg_match_all ($re, $str, $match);
        
 
        $param = array();
        
        foreach($match[0] as $k=>$v){

            $s = strpos($v,'=');

            if ($s!==false){
                $key = substr($v,0,$s);
                $param[$key] = substr($v,$s+2,strlen($v)-$s-3);
            }else{
                $param[$v] = true;
            }

        }
       
        return $param;
    }


    
}       